package com.clz.appdesign.mvvm;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import com.clz.appdesign.appdependence.net.RetrofitFactory;
import com.clz.appdesign.appdependence.net.RxSchedulers;
import com.clz.appdesign.myapp.utils.L;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @author chenliangzhi
 * @date 2018/9/20
 * @describe
 */

public class ImageViewModel extends ViewModel {
    private MutableLiveData<Data<ImageBean.ImagesBean>> mImage = new MutableLiveData<>();
    private int idx;

    public MutableLiveData<Data<ImageBean.ImagesBean>> getImage() {
        return mImage;
    }

    public void loadImage() {
        RetrofitFactory.getInstence().createService(TestApiService.class)
                .getImages("js", idx, 1)
                .compose(RxSchedulers.io_main())
                .subscribe(new Observer<ImageBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ImageBean imageBean) {
                        L.i("image2",imageBean.toString());
                        mImage.setValue(new Data<ImageBean.ImagesBean>(imageBean.getImages().get(0), null));
                    }

                    @Override
                    public void onError(Throwable e) {
                        mImage.setValue(new Data<ImageBean.ImagesBean>(
                                null, e.getMessage()
                        ));
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void nextImage() {
        RetrofitFactory.getInstence().createService(TestApiService.class)
                .getImages("js", ++idx, 1)
                .compose(RxSchedulers.io_main())
                .subscribe(new Observer<ImageBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ImageBean imageBean) {
                        mImage.setValue(new Data<ImageBean.ImagesBean>(imageBean.getImages().get(0), null));
                    }

                    @Override
                    public void onError(Throwable e) {
                        mImage.setValue(new Data<ImageBean.ImagesBean>(
                                null, e.getMessage()
                        ));
                        idx--;
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void privousImage() {
        RetrofitFactory.getInstence().createService(TestApiService.class)
                .getImages("js", --idx, 1)
                .compose(RxSchedulers.io_main())
                .subscribe(new Observer<ImageBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ImageBean imageBean) {
                        mImage.setValue(new Data<ImageBean.ImagesBean>(imageBean.getImages().get(0), null));
                    }

                    @Override
                    public void onError(Throwable e) {
                        mImage.setValue(new Data<ImageBean.ImagesBean>(
                                null, e.getMessage()
                        ));
                        idx++;
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
